<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="ordernum">
        <el-input
          v-model="queryParams.ordernum"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属门店" prop="mid">
          <el-select v-model="queryParams.mid" placeholder="请选择门店" clearable>
            <el-option
              v-for="item in mds"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hatha:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hatha:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="订单号" align="center" prop="ordernum" />
      <el-table-column label="微信订单号" align="center" prop="wxorder" />
      <el-table-column label="用户" align="center" prop="uid">
        <template slot-scope="scope">
          <span style="color: blue;cursor: pointer;" @click="viewuserinfo(scope.row.uid)">{{scope.row.sysUser.nickName+'[ID:'+scope.row.uid+']'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单类型" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type==1">课程订单</span>
          <span v-if="scope.row.type==2">套餐订单</span>
          <span v-if="scope.row.type==3">会员卡订单</span>
        </template>
      </el-table-column>
      <el-table-column label="项目" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type==1&&scope.row.kecheng!=null">{{scope.row.kecheng.name}}</span>
          <span v-if="scope.row.type==2&&scope.row.taocan!=null">{{scope.row.taocan.name}}</span>
          <span v-if="scope.row.type==3&&scope.row.cards!=null">{{scope.row.cards.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="ispay">
        <template slot-scope="scope">
          <span style="color: red;" v-if="scope.row.ispay==0">未支付</span>
          <span style="color: green;" v-if="scope.row.ispay==1">已支付</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="state" /> -->
      <el-table-column label="支付时间" align="center" prop="paytime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paytime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createtim" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtim, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="jine" />
      <!-- <el-table-column label="openid" align="center" prop="openid" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hatha:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewinfo(scope.row)"
            v-hasPermi="['hatha:order:remove']"
          >使用情况</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="ordernum">
          <el-input v-model="form.ordernum" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="课程ID" prop="kcid">
          <el-input v-model="form.kcid" placeholder="请输入课程ID" />
        </el-form-item>
        <el-form-item label="套餐ID" prop="tcid">
          <el-input v-model="form.tcid" placeholder="请输入套餐ID" />
        </el-form-item>
        <el-form-item label="会员卡ID" prop="ccid">
          <el-input v-model="form.ccid" placeholder="请输入会员卡ID" />
        </el-form-item>
        <el-form-item label="微信订单号" prop="wxorder">
          <el-input v-model="form.wxorder" placeholder="请输入微信订单号" />
        </el-form-item>
        <el-form-item label="用户ID" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="支付" prop="ispay">
          <el-input v-model="form.ispay" placeholder="请输入支付" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="支付时间" prop="paytime">
          <el-date-picker clearable
            v-model="form.paytime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建时间" prop="createtim">
          <el-date-picker clearable
            v-model="form.createtim"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="金额 分" prop="jine">
          <el-input v-model="form.jine" placeholder="请输入金额 分" />
        </el-form-item>
        <el-form-item label="openid" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入openid" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog
      v-if="userinfo!=null"
      title="用户信息"
      :visible.sync="showuser"
      width="50%"
      :before-close="userclose">
      <div>
        <el-row :gutter="20">
          <el-col :span="6">
            <div>
              <el-statistic title="名称">
                <template slot="formatter">
                  {{userinfo.userinfo.nickName}}
                </template>
              </el-statistic>
            </div>
          </el-col>
          <el-col :span="6">
            <div>
              <el-statistic title="手机号">
                <template slot="formatter">
                  {{userinfo.userinfo.phonenumber}}
                </template>
              </el-statistic>
            </div>
          </el-col>
          <el-col :span="6">
            <div>
              <el-statistic title="用户ID">
                <template slot="formatter">
                  {{userinfo.userinfo.userId}}
                </template>
              </el-statistic>
            </div>
          </el-col>
          <el-col :span="6">
            <div>
              <el-statistic title="上次IP">
                <template slot="formatter">
                  {{userinfo.userinfo.loginIp}}
                </template>
              </el-statistic>
            </div>
          </el-col>
        </el-row>
        <div style="margin-top: 40px;">
          <el-row>
            <el-tabs type="border-card">
              <el-tab-pane label="会员卡">
                <el-table :data="userinfo.cards">
                  <el-table-column property="card.name" label="卡项"></el-table-column>
                  <el-table-column property="status" label="状态">
                    <template slot-scope="scope">
                      <span>{{ scope.row.status==0?'正常':'暂停' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column property="sycs" label="次数"></el-table-column>
                  <el-table-column property="endTime" label="到期日"></el-table-column>
                </el-table>
              </el-tab-pane>
              <el-tab-pane label="课程">
                <el-table :data="userinfo.kecheng">
                  <el-table-column property="kecheng.name" label="课程" ></el-table-column>
                  <el-table-column property="isused" label="状态" >
                    <template slot-scope="scope">
                      <span>{{ scope.row.isused==0?'未使用':'已使用' }}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
              <el-tab-pane label="套餐">
                <el-table :data="userinfo.taocan">
                  <el-table-column property="taocan.name" label="套餐"></el-table-column>
                  <el-table-column property="taocan.jiage" label="价格"></el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
          </el-row>
        </div>
        
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="userclose">确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder,userInfo } from "@/api/hatha/order";
import { listMendian } from "@/api/hatha/mendian";
export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      orderList: [],
      showuser:false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ordernum: null,
        kcid: null,
        tcid: null,
        ccid: null,
        wxorder: null,
        uid: null,
        type: null,
        ispay: null,
        state: null,
        paytime: null,
        createtim: null,
        jine: null,
        openid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      userinfo:null,
      mds:[]
    };
  },
  created() {
    this.getList();
    this.getMendian()
  },
  methods: {
    getMendian(){
      listMendian().then(res => {
        this.mds = res.rows
      })
    },
    viewuserinfo(e){
      console.log(e)
      this.loading = true;
      userInfo(e).then(response => {
        this.userinfo = response.data;
        this.showuser = true
        this.loading = false;
      });
    },
    userclose(){
      this.showuser = false
    },
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ordernum: null,
        kcid: null,
        tcid: null,
        ccid: null,
        wxorder: null,
        uid: null,
        type: null,
        ispay: null,
        state: null,
        paytime: null,
        createtim: null,
        jine: null,
        openid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hatha/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
